#include "stdafx.h"
#include "IAddWeighted.h"
//OpenCV libs
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"

const char * const IAddWeighted::FunctionNameIAddWeighted = "Add(Weighted)";

IAddWeighted::IAddWeighted(void) 
{
	_fnApi.SetFunctionName(IAddWeighted::FunctionNameIAddWeighted);
	_fnApi.SetFunctionDescription("This function adds a image on top of the solution according to their weights.");
									//inputs
	_fnApi.AddParamDef(new ProcParamDefString(0, "Image", "Sets the Image to be added. Make sure the dimensions are the same as that of the image you're adding to. Enter the path to the image", ProcParamDef::input, 0, 500 ));
	_fnApi.AddParamDef(new ProcParamDefDouble(1, "WeightA", "Set the weight of the image to be added", ProcParamDef::input, 0, 100 ));
	_fnApi.AddParamDef(new ProcParamDefDouble(2, "WeightS", "Set the weight of the image you're adding to", ProcParamDef::input, 0, 100 ));
	_fnApi.AddParamDef(new ProcParamDefDouble(3, "Gamma", "Set the scalar, added to the sum", ProcParamDef::input, 0, 10 ));
									//outputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, ProcParam::attribOutputParamNameResult, "0 is success", ProcParamDef::output, 0, 1));

	_fnApi.Validate();
}


IAddWeighted::~IAddWeighted(void)
{

}


bool IAddWeighted::Process(ImageProcStep *step, bool DebugMode)
{	
	bool rc = false;
	if ((step != '\0') && (step->IsValid()))
	{
		
		ProcParamString *source = dynamic_cast<ProcParamString *>(step->GetInputs()->GetParam(0));	//Check the Param number
		ProcParamDouble *alpha = dynamic_cast<ProcParamDouble *>(step->GetInputs()->GetParam(1));
		ProcParamDouble *beta = dynamic_cast<ProcParamDouble *>(step->GetInputs()->GetParam(2));
		ProcParamDouble *gamma = dynamic_cast<ProcParamDouble *>(step->GetInputs()->GetParam(3));
		if  (source != '\0' && alpha != '\0' && beta != '\0' && gamma != '\0' )
		{
			cv::Mat output;
			cv::Mat input = cv::imread(step->GetIntermediateImagePathFilename());	
			cv::Mat input2 = cv::imread((std::string)source->GetValueStr());
			cv::addWeighted(input, double(alpha->GetValue()), input2, double(beta->GetValue()), double(gamma->GetValue()), output);

			cv::imwrite(step->GetIntermediateImagePathFilename(), output);

			input.release();
			input2.release();
			output.release();
			rc = true;
		}
	}
	return rc;
}



